<script setup>
import {
  Management,
  Promotion,
  UserFilled,
  User,
  Crop,
  EditPen,
  SwitchButton,
  CaretBottom,
  Memo
} from '@element-plus/icons-vue'
import avatar from '@/assets/default.png'
import {userInfoService} from '@/api/user/user.js'
import {getRoleListService} from '@/api/system/user.js'
import {useUserInfoStore} from '@/stores/userInfo.js'
import {useTokenStore} from '@/stores/token.js'
import {useRouter} from "vue-router";
import {ElMessage, ElMessageBox} from "element-plus";
import {ref} from "vue";


//获得用户信息Store
const userInfoStore=useUserInfoStore()
//获得token store
const tokenStore=useTokenStore()
//获得路由器
const router=useRouter()
//角色信息
const roleList=ref([])


//获取用户详细信息
const getUserInfo= async ()=>{
  let result=await userInfoService()
  userInfoStore.setUserInfo(result.data)
}
getUserInfo()

//获取用户角色信息
const getRoleList= async ()=>{
  let result=await getRoleListService()
  roleList.value=result.data
}
getRoleList()

//判断用户是否不是普通用户
const isNotCommon= ()=>{
  for (let i = 0; i < roleList.value.length; i++) {
    if(roleList.value[i].roleKey!=='common'){
      return true
    }
  }
  return false
}

//下拉菜单点击处理
const handleDropdown=(command)=>{
  if(command=='logout'){
    ElMessageBox.confirm(
        '你确定要退出登录吗?',
        '温馨提示',
        {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning',
        }
    )
        .then( async () => {
          //清空pinia中的token和userInfo
          tokenStore.removeToken()
          userInfoStore.removeUserInfo()
          //跳到登录页面
          router.push('/login')
          ElMessage({
            type: 'success',
            message: '退出成功',
          })
        })
        .catch(() => {
          ElMessage({
            type: 'info',
            message: '取消退出',
          })
        })
  }else {
    //路由
    router.push('/user/'+command)
  }
}
</script>

<template>
  <el-container class="layout-container">
    <!-- 左侧菜单 -->
    <el-aside width="200px">
      <div class="el-aside__logo"></div>
      <el-menu active-text-color="#ffd04b" background-color="#232323"  text-color="#fff"
               router unique-opened="true">
        <el-menu-item index="/article/category">
          <el-icon>
            <Management />
          </el-icon>
          <span>文章分类</span>
        </el-menu-item>
        <el-menu-item index="/article/manage">
          <el-icon>
            <Promotion />
          </el-icon>
          <span>文章管理</span>
        </el-menu-item>
        <el-sub-menu index="1">
          <template #title >
            <el-icon>
              <UserFilled />
            </el-icon>
            <span>个人中心</span>
          </template>
          <el-menu-item index="/user/info">
            <el-icon>
              <User />
            </el-icon>
            <span>基本资料</span>
          </el-menu-item>
          <el-menu-item index="/user/avatar">
            <el-icon>
              <Crop />
            </el-icon>
            <span>更换头像</span>
          </el-menu-item>
          <el-menu-item index="/user/resetPassword">
            <el-icon>
              <EditPen />
            </el-icon>
            <span>重置密码</span>
          </el-menu-item>
        </el-sub-menu>
        <el-sub-menu index="2" v-if="isNotCommon()">
          <template #title>
            <el-icon>
              <Memo />
            </el-icon>
            <span>后台管理</span>
          </template>
          <el-menu-item index="/sys/userManage">
            <el-icon>
              <User />
            </el-icon>
            <span>用户管理</span>
          </el-menu-item>
          <el-menu-item index="/sys/categoryManage">
            <el-icon>
              <Management />
            </el-icon>
            <span>文章分类管理</span>
          </el-menu-item>
          <el-menu-item index="/sys/articleManage">
            <el-icon>
              <Promotion />
            </el-icon>
            <span>文章管理</span>
          </el-menu-item>
        </el-sub-menu>
      </el-menu>
    </el-aside>
    <!-- 右侧主区域 -->
    <el-container>
      <!-- 头部区域 -->
      <el-header>
        <div>用户昵称：<strong>{{ userInfoStore.userInfo.nickname }}</strong></div>
        <!-- command事件，会在下拉菜单点击后调用 -->
        <el-dropdown placement="bottom-end" @command="handleDropdown">
                    <span class="el-dropdown__box">
                        <el-avatar :src="userInfoStore.userInfo.userPic ? userInfoStore.userInfo.userPic:avatar" />
                        <el-icon>
                            <CaretBottom />
                        </el-icon>
                    </span>
          <template #dropdown>
            <el-dropdown-menu>
              <!-- command事件调用的函数会通过command属性传参 -->
              <el-dropdown-item command="info" :icon="User">基本资料</el-dropdown-item>
              <el-dropdown-item command="avatar" :icon="Crop">更换头像</el-dropdown-item>
              <el-dropdown-item command="resetPassword" :icon="EditPen">重置密码</el-dropdown-item>
              <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </el-header>
      <!-- 中间区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
      <!-- 底部区域 -->
      <el-footer>大事件 ©2023 Created by 黑马程序员</el-footer>
    </el-container>
  </el-container>
</template>

<style lang="scss" scoped>
.layout-container {
  height: 100vh;

  .el-aside {
    background-color: #232323;

    &__logo {
      height: 120px;
      background: url('@/assets/logo.png') no-repeat center / 120px auto;
    }

    .el-menu {
      border-right: none;
    }
  }

  .el-header {
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .el-dropdown__box {
      display: flex;
      align-items: center;

      .el-icon {
        color: #999;
        margin-left: 10px;
      }

      &:active,
      &:focus {
        outline: none;
      }
    }
  }

  .el-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    color: #666;
  }
}
</style>